package com.itheima.controller;

import com.itheima.domain.vo.Result;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ghy
 * @version 1.0
 * @since 2025-07-17 09:46
 */
@RestController
@Slf4j
public class CookieController {

    @GetMapping("/c1")
    public Result c1(HttpServletResponse response){
        response.addCookie(new Cookie("login_name", "jack"));
        response.addCookie(new Cookie("gender", "1"));
        return Result.success();
    }

    @GetMapping("/c2")
    public Result c1(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            System.out.println(cookie.getName() + ":" + cookie.getValue());
        }
        return Result.success();
    }


    @GetMapping("/s1")
    public Result s1(HttpSession session){
        //第一次：会判断是否有session，没有则创建
        System.out.println("c1:" + session.getId());
        session.setAttribute("username", "tom");
        return Result.success();
    }

    @GetMapping("/s2")
    public Result s2(HttpSession session){
        System.out.println("c2:" + session.getId());
        Object username = session.getAttribute("username");
        System.out.println("c2:" + username);
        return Result.success();
    }

}
